package com.cch.cooperation.api.zone.controller;

import com.cch.cooperation.api.auth.dto.LoginUserDTO;
import com.cch.cooperation.api.common.request.IdReq;
import com.cch.cooperation.api.zone.dto.ZoneEditDTO;
import com.cch.cooperation.api.zone.service.ZoneServiceImpl;
import com.cch.cooperation.common.web.response.Response;
import com.cch.cooperation.common.web.session.Session;
import com.cch.cooperation.common.web.session.SessionManager;
import com.cch.cooperation.dao.zone.dto.ZoneDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 空间接口
 *
 * @author cch
 */
@Api(tags = "空间接口")
@RestController
@RequestMapping("/api/zone")
public class ZoneController {

    @Resource
    private ZoneServiceImpl zoneService;
    @Resource
    private SessionManager sessionManager;

    @ApiOperation("查询空间列表")
    @GetMapping("/queryList")
    public Response<List<ZoneDTO>> queryList(@ApiParam(value = "type", required = true) Integer type) {
        Session<LoginUserDTO> session = sessionManager.get();
        List<ZoneDTO> data = zoneService.queryList(session.getData().getId(), type);
        return Response.success(data);
    }

    @ApiOperation("根据id获取空间详情")
    @GetMapping("/getById")
    public Response<ZoneDTO> getById(@ApiParam(value = "id", required = true) String id) {
        ZoneDTO data = zoneService.getById(id);
        return Response.success(data);
    }

    @ApiOperation("根据邀请码获取空间详情")
    @GetMapping("/getByJoinCode")
    public Response<ZoneDTO> getByJoinCode(@ApiParam(value = "joinCode", required = true) String joinCode) {
        ZoneDTO data = zoneService.getByJoinCode(joinCode);
        return Response.success(data);
    }

    @ApiOperation("新增空间")
    @PostMapping("/add")
    public Response<ZoneDTO> add(@RequestBody ZoneEditDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setUpdateUserId(session.getData().getId());
        ZoneDTO data = zoneService.add(req);
        return Response.success(data);
    }

    @ApiOperation("修改空间")
    @PostMapping("/updateById")
    public Response<ZoneDTO> updateById(@RequestBody ZoneEditDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setUpdateUserId(session.getData().getId());
        ZoneDTO data = zoneService.updateById(req);
        return Response.success(data);
    }

    @ApiOperation("删除空间")
    @PostMapping("/deleteById")
    public Response<Void> deleteById(@RequestBody IdReq req) {
        zoneService.deleteById(req.getId());
        return Response.success();
    }

}
